package com.zhzhinn.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzBaseTooolBarActivity;
import com.edgclub.edg.activity.GzLoginActivity;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.activity.RegisterSetPassActivity;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.UserData;
import com.edgclub.edg.utils.util.CryptUtil;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

public class ZZSetPassWordActivity extends GzBaseTooolBarActivity implements View.OnClickListener, VolleyListener {
    private EditText phone, mess_code, password;
    private Button get_code_btn;

    private int time = 120;
    private Timer timer;
    private TimerTask timerTask;
    private UserPrefUtils userPrefUtils;

    @Override
    public int getContentView() {
        return R.layout.zz_activity_setpassword;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("忘记密码");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
        right_text.setText("确定");
        right_layout.setVisibility(View.VISIBLE);
        right_layout.setOnClickListener(this);
    }

    @Override
    public void initView() {
        phone = (EditText) findViewById(R.id.phone);
        mess_code = (EditText) findViewById(R.id.mess_code);
        password = (EditText) findViewById(R.id.password);
        get_code_btn = (Button) findViewById(R.id.get_code_btn);

        get_code_btn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.right_layout:
                hideSoftKeyboard();
                oksubmit();
                break;
            case R.id.main_back:
                finish();
                break;
            case R.id.get_code_btn:
                getCode();
                break;
        }
    }

    private void getCode() {
        String strPhone = phone.getText().toString().trim();
        if (TextUtils.isEmpty(strPhone)) {
            Toast.makeText(ZZSetPassWordActivity.this, "请输入你的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!StringUtils.isPhoneNum(strPhone)) {
            Toast.makeText(ZZSetPassWordActivity.this, "手机格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        UserParam param = new UserParam();
        param.setUid(strPhone);
        param.setTarget("forgetPwd");
        new UserApi(this).registerGetcode(param, this);

    }

    public void oksubmit() {
        if (TextUtils.isEmpty(phone.getText().toString().trim())) {
            Toast.makeText(ZZSetPassWordActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mess_code.getText().toString().trim())) {
            Toast.makeText(ZZSetPassWordActivity.this, "请输入收到的验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password.getText().toString().trim())) {
            Toast.makeText(ZZSetPassWordActivity.this, "请输入新的密码", Toast.LENGTH_SHORT).show();
            return;
        }

//        String strPhone = phone.getText().toString().trim();
//        UserParam param = new UserParam();
//        param.setUid(strPhone);
//        if ("1".equals(type)){
//            param.setTarget("forgetPwd");
//            param.setCode(mess_code.getText().toString().trim());
//        }
//        else {
//            param.setTarget("register");
//            param.setCode(mess_code.getText().toString().trim());
//        }
//        new UserApi(this).registerCode(param,this);
        String strpsw = password.getText().toString().trim();
        UserParam param = new UserParam();
        param.setVerifyCode(mess_code.getText().toString().trim());
        param.setPhone(phone.getText().toString().trim());
        param.setPassword(CryptUtil.md5(strpsw));
        new UserApi(this).forgetPsw(param, this);
    }

    @Override
    public void onStart(String tag) {
        if (ApiConfig.APIURL.URL_REGISTER_CODE.equals(tag)) {
            showDialog(StringUtils.getString(R.string.get_identifying), false);
        }
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (ApiConfig.APIURL.URL_REGISTER_CODE.equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                Toast.makeText(ZZSetPassWordActivity.this, StringUtils.getString(R.string.checkverification),
                        Toast.LENGTH_LONG).show();
                get_code_btn.setEnabled(false);
                startTimer();

            } else {
                Toast.makeText(
                        ZZSetPassWordActivity.this,
                        StringUtils.getString(R.string.verificationfail) + ":code(" + JSONHelper.getResultCode(response)
                                + ")", Toast.LENGTH_LONG).show();
            }
        } else if (ApiConfig.APIURL.URL_FORGET_PSW.equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                UserData userData = JsonUtil.getMode(response, UserData.class);
                if (null != userData && null != userData.getData()) {
                    userData.getData().setPhone(phone.getText().toString().trim());
                    userPrefUtils.saveUser(userData.getData());
                }
                userPrefUtils.saveName(phone.getText().toString().trim());
                userPrefUtils.savePSW(CryptUtil.md5(password.getText().toString().trim()));
                Toast.makeText(this, "重置密码成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(
                        ZZSetPassWordActivity.this,
                        JSONHelper.getResultMsg(response)
                        , Toast.LENGTH_LONG).show();
            }

        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        ResultCode.toastVolleyError(ZZSetPassWordActivity.this, volleyError);
    }

    class CountDownTimerTask extends TimerTask {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }

    private void startTimer() {
        time = 60;
        get_code_btn.setTextColor(getResources().getColor(R.color.c5));
        get_code_btn.setBackgroundResource(R.drawable.zz_login_shape_bg9);
        get_code_btn.setText(time + "");
        if (null == timer) {
            timer = new Timer();
        }

        timerTask = new CountDownTimerTask();
        timer.schedule(timerTask, 1000, 1000);
    }

    private void stopTimer() {
        get_code_btn.setTextColor(getResources().getColor(R.color.c7));
        get_code_btn.setBackgroundResource(R.drawable.zz_login_shape_bg2);
        if (null != timer) {
            timer.cancel();
            timer = null;
        }

        if (null != timerTask) {
            timerTask.cancel();
        }

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    time--;
                    get_code_btn.setText(time + "");
                    if (time == 0) {
                        stopTimer();
                        time = 60;
                        get_code_btn.setEnabled(true);
                        get_code_btn.setText("获取");
                    }
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };
}
